﻿/*******************************************************************************
 * Copyright (c) 2000, 2003 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials 
 * are made available under the terms of the Common Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
module dwt.events.events;


private import dwt.dwt;

private import dwt.events.typedevent;
private import dwt.events.listeners;
private import dwt.widgets.event;
private import dwt.graphics.gc;
private import dwt.widgets.widget;





/**
 * Instances of this class are sent as a result of
 * a widget such as a menu item being armed.
 *
 * @see ArmListener
 */

public class ArmEvent : TypedEvent {

/**
 * Constructs a new instance of this class based on the
 * information in the given untyped event.
 *
 * @param e the untyped event containing the information
 */
public this(Event e) {
	super(e);
}

}




/**
 * Instances of this class are sent as a result of
 * controls being moved or resized.
 *
 * @see ControlListener
 */

public class ControlEvent : TypedEvent {

/**
 * Constructs a new instance of this class based on the
 * information in the given untyped event.
 *
 * @param e the untyped event containing the information
 */
public this(Event e) {
	super(e);
}

}



/**
 * Instances of this class are sent as a result of
 * widgets being disposed.
 *
 * @see DisposeListener
 */

public class DisposeEvent : TypedEvent {

/**
 * Constructs a new instance of this class based on the
 * information in the given untyped event.
 *
 * @param e the untyped event containing the information
 */
public this(Event e) {
	super(e);
}

}


/**
 * Instances of this class are sent as a result of
 * widgets gaining and losing focus.
 *
 * @see FocusListener
 */

public class FocusEvent : TypedEvent {

/**
 * Constructs a new instance of this class based on the
 * information in the given untyped event.
 *
 * @param e the untyped event containing the information
 */
public this(Event e) {
	super(e);
}

}


/**
 * Instances of this class are sent as a result of
 * help being requested for a widget.
 *
 * @see HelpListener
 */

public class HelpEvent : TypedEvent {

/**
 * Constructs a new instance of this class based on the
 * information in the given untyped event.
 *
 * @param e the untyped event containing the information
 */
public this(Event e) {
	super(e);
}

}





/**
 * Instances of this class are sent as a result of
 * keys being pressed and released on the keyboard
 *
 * @see KeyListener
 */

public class KeyEvent : TypedEvent {
	
 	/**
 	 * the character represented by the key that was typed.  
	 * This is the final character that results after all modifiers have been
 	 * applied.  For example, when the user types Ctrl+A, the  value
 	 * is 0x01.  It is important that applications do not attempt to modify the
 	 *  value based on a stateMask (such as DWT.CTRL) or the resulting
 	 *  will not be correct.
 	 */
 	/**
 	 * <Shawn> since D init wchar as 0xFFFF, init to '\0' explicitly here 
 	 */
	public wchar character = '\0';
	
	/**
	 * the key code of the key that was typed,
	 * as defined by the key code constants in class <code>DWT</code>.
	 * When the  field of the event is ambiguous, this field
	 * contains the unicode value of the original character.  For example,
	 * typing Ctrl+M or Return both result in the character '\r' but the
	 * keyCode field will also contain '\r' when Return was typed.
	 * 
	 * @see org.eclipse.dwt.DWT
	 */
	public int keyCode;
	
	/**
	 * the state of the keyboard modifier keys at the time
	 * the event was generated
	 */
	public int stateMask;
	
	/**
	 * A flag indicating whether the operation should be allowed.
	 * Setting this field to <code>false</code> will cancel the operation.
	 */
	public boolean doit;

/**
 * Constructs a new instance of this class based on the
 * information in the given untyped event.
 *
 * @param e the untyped event containing the information
 */
public this(Event e) {
	super(e);
	this.character = e.character;
	this.keyCode = e.keyCode;
	this.stateMask = e.stateMask;
	this.doit = e.doit;
}

/**
 * Returns a string containing a concise, human-readable
 * description of the receiver.
 *
 * @return a string representation of the event
 */
public char[] toString() {
	
	char[] string = super.toString()[0..--$];// remove trailing '}'
	
	// <Shawn> when (character == 0), the string will be truncated as null terminated
	// so convert character to string first
	char[] chars;
	if(character == 0)
		chars = "\\0";
	else{
		wchar[] ws; 
		ws ~= character;
		chars = Utf.toString(ws);
	}
	
	string = 
		string ~ " character='"  ~ chars ~  "'"
		~ " keyCode=" ~ Int.toString( keyCode )
		~ " stateMask="~ Int.toString(  stateMask )
		~ " doit=" ~ Int.toString( doit )
		~ "}";
	return string;
}

}



/**
 * Instances of this class are sent as a result of
 * menus being shown and hidden.
 *
 * @see MenuListener
 */

public class MenuEvent : TypedEvent {

/**
 * Constructs a new instance of this class based on the
 * information in the given untyped event.
 *
 * @param e the untyped event containing the information
 */
public this(Event e) {
	super(e);
}

}




/**
 * Instances of this class are sent as a result of
 * text being modified.
 *
 * @see ModifyListener
 */

public class ModifyEvent : TypedEvent {

/**
 * Constructs a new instance of this class based on the
 * information in the given untyped event.
 *
 * @param e the untyped event containing the information
 */
public this(Event e) {
	super(e);
}

}






/**
 * Instances of this class are sent whenever mouse
 * related actions occur. This includes mouse buttons
 * being pressed and released, the mouse pointer being 
 * moved and the mouse pointer crossing widget boundaries.
 * <p>
 * Note: The <code>button</code> field is an integer that
 * represents the mouse button number.  This is not the same
 * as the <code>DWT</code> mask constants <code>BUTTONx</code>.
 * </p>
 *
 * @see MouseListener
 * @see MouseMoveListener
 * @see MouseTrackListener
 */

public class MouseEvent : TypedEvent {
	
	/**
	 * the button that was pressed or released; 1 for the
	 * first button, 2 for the second button, and 3 for the
	 * third button, etc.
	 */
	public int button;
	
	/**
	 * the state of the keyboard modifier keys at the time
	 * the event was generated
	 */
	public int stateMask;
	
	/**
	 * the widget-relative, x coordinate of the pointer
	 * at the time the mouse button was pressed or released
	 */
	public int x;
	
	/**
	 * the widget-relative, y coordinate of the pointer
	 * at the time the mouse button was pressed or released
	 */	
	public int y;

/**
 * Constructs a new instance of this class based on the
 * information in the given untyped event.
 *
 * @param e the untyped event containing the information
 */
public this(Event e) {
	super(e);
	this.x = e.x;
	this.y = e.y;
	this.button = e.button;
	this.stateMask = e.stateMask;
}

/**
 * Returns a string containing a concise, human-readable
 * description of the receiver.
 *
 * @return a string representation of the event
 */
public char[] toString() {
	
	char[] string = super.toString()[0..--$];// remove trailing '}'
	char[] s = string
		~ " button=" ~  Int.toString( button )
		~ " stateMask=" ~ Int.toString( stateMask )
		~ " x=" ~ Int.toString( x )
		~ " y=" ~ Int.toString( y )
		~ "}";
	return s;
}

}



/**
 * Instances of this class are sent as a result of
 * visible areas of controls requiring re-painting.
 *
 * @see PaintListener
 */

public class PaintEvent : TypedEvent {
	
	/**
	 * the graphics context to use when painting
	 * that is configured to use the colors, font and
	 * damaged region of the control.  It is valid
	 * only during the paint and must not be disposed
	 */
	public GC gc = null;
	
	/**
	 * the x offset of the bounding rectangle of the 
	 * region that requires painting
	 */
	public int x;
	
	/**
	 * the y offset of the bounding rectangle of the 
	 * region that requires painting
	 */
	public int y;
	
	/**
	 * the width of the bounding rectangle of the 
	 * region that requires painting
	 */
	public int width;
	
	/**
	 * the height of the bounding rectangle of the 
	 * region that requires painting
	 */
	public int height;

	/**
	 * the number of following paint events which
     * are pending which may always be zero on
	 * some platforms
	 */
	public int count;

/**
 * Constructs a new instance of this class based on the
 * information in the given untyped event.
 *
 * @param e the untyped event containing the information
 */
public this(Event e) {
	super(e);
	this.gc = e.gc;
	this.x = e.x;
	this.y = e.y;
	this.width = e.width;
	this.height = e.height;
	this.count = e.count;
}

/**
 * Returns a string containing a concise, human-readable
 * description of the receiver.
 *
 * @return a string representation of the event
 */
public char[] toString() {
	
	char[] string = (super.toString())[0..--$];// remove trailing '}'	
	char[] s = string 
	~ " gc=" ~ ( gc ? gc.toString() : "null")
	~" x=" ~ Int.toString( x )		
	~ " y=" ~Int.toString(  y )
	~ " width=" ~Int.toString( width )	
	~" height=" ~Int.toString( height )		
	~" count=" ~Int.toString( count )		
	~"}";
	return s;
}
}




/**
 * Instances of this class are sent as a result of
 * widgets being selected.
 * <p>
 * Note: The fields that are filled in depend on the widget.
 * </p>
 *
 * @see SelectionListener
 */

public class SelectionEvent : TypedEvent {
	
	/**
	 * The item that was selected.
	 */
	public Widget item = null;
	
	/**
	 * Extra detail information about the selection, depending on the widget.
	 * 
	 * <p><b>Sash</b><ul>
	 * <li>{@link org.eclipse.dwt.DWT#DRAG}</li>
	 * </ul></p><p><b>ScrollBar and Slider</b><ul>
	 * <li>{@link org.eclipse.dwt.DWT#DRAG}</li>
	 * <li>{@link org.eclipse.dwt.DWT#HOME}</li>
	 * <li>{@link org.eclipse.dwt.DWT#END}</li>
	 * <li>{@link org.eclipse.dwt.DWT#ARROW_DOWN}</li>
	 * <li>{@link org.eclipse.dwt.DWT#ARROW_UP}</li>
	 * <li>{@link org.eclipse.dwt.DWT#PAGE_DOWN}</li>
	 * <li>{@link org.eclipse.dwt.DWT#PAGE_UP}</li>
	 * </ul></p><p><b>Table, Tree and TableTree</b><ul>
	 * <li>{@link org.eclipse.dwt.DWT#CHECK}</li>
	 * </ul></p><p><b>CoolItem and ToolItem</b><ul>
	 * <li>{@link org.eclipse.dwt.DWT#ARROW}</li>
	 * </ul></p>
	 */
	public int detail;

	/**
	 * The x location of the selected area.
	 */
	public int x;
	
	/**
	 * The y location of selected area.
	 */
	public int y;
	
	/**
	 * The width of selected area.
	 */
	public int width;
	
	/**
	 * The height of selected area.
	 */
	public int height;

	/**
	 * The state of the keyboard modifier keys at the time
	 * the event was generated.
	 */
	public int stateMask;

	/**
	 * A flag indicating whether the operation should be allowed.
	 * Setting this field to <code>false</code> will cancel the
	 * operation, depending on the widget.
	 */
	public boolean doit;
	
/**
 * Constructs a new instance of this class based on the
 * information in the given untyped event.
 *
 * @param e the untyped event containing the information
 */
public this(Event e) {
	super(e);
	this.item = e.item;
	this.x = e.x;
	this.y = e.y;
	this.width = e.width;
	this.height = e.height;
	this.detail = e.detail;
	this.stateMask = e.stateMask;
	this.doit = e.doit;
}

/**
 * Returns a string containing a concise, human-readable
 * description of the receiver.
 *
 * @return a string representation of the event
 */
public char[] toString() {
	
	char[] string = (super.toString())[0..--$];// remove trailing '}'

	char[] s = string 
		~ " item="  ~ (item ? item.toString() : "null")
		~ " detail=" ~ Int.toString(detail)
		~ " x=" ~ Int.toString(  x)
		~ " y=" ~ Int.toString( y)
		~" width=" ~Int.toString( width)
		~ " height=" ~Int.toString(  height )
		~ " stateMask=" ~  Int.toString( stateMask)
		~" doit="  ~ Int.toString(doit)
		~ "}";
	return s;
}
}




/**
 * Instances of this class are sent as a result of
 * operations being performed on shells.
 *
 * @see ShellListener
 */

public class ShellEvent : TypedEvent {

	/**
	 * A flag indicating whether the operation should be allowed.
	 * Setting this field to <code>false</code> will cancel the operation.
	 */
	public boolean doit;
	
/**
 * Constructs a new instance of this class based on the
 * information in the given untyped event.
 *
 * @param e the untyped event containing the information
 */
public this(Event e) {
	super(e);
	this.doit = e.doit;
}

/**
 * Returns a string containing a concise, human-readable
 * description of the receiver.
 *
 * @return a string representation of the event
 */
public char[] toString() {
	
	char[] string = (super.toString())[0..--$];// remove trailing '}'
	char[] s = string ~ " doit="~  Int.toString(doit) ~ "}";

	return s;
}
}




/**
 * Instances of this class are sent as a result of
 * widget traversal actions.
 * <p>
 * The traversal event allows fine control over keyboard traversal
 * in a control both to implement traversal and override the default
 * traversal behavior defined by the system.  This is achieved using
 * two fields, <code>detail</code> and <code>doit</code>.
 * </p><p>
 * When a control is traversed, a traverse event is sent.  The detail
 * describes the type of traversal and the doit indicates the default
 * behavior of the system.  For example, when a right arrow key is pressed
 * in a text control, the detail field is <code>TRAVERSE_ARROW_NEXT</code>
 * and the doit field is <code>false</code>, indicating that the system
 * will not traverse to the next tab item and the arrow key will be
 * delivered to the text control.  If the same key is pressed in a radio
 * button, the doit field will be <code>true</code>, indicating that
 * traversal is to proceed to the next tab item, possibly another
 * radio button in the group and that the arrow key is not be delivered
 * to the radio button.
 * </p><p>
 * How can the traversal event be used to implement traversal?
 * When a tab key is pressed in a canvas, the detail field will be
 * <code>TRAVERSE_TAB_NEXT</code> and the doit field will be
 * <code>false</code>.  The default behavior of the system is to
 * provide no traversal for canvas controls.  This means that by
 * default in a canvas, a key listener will see every key that
 * user types, including traversal keys.  To understand why this
 * is so, it is important to understand that only the widget implementor
 * can decide which traversal is appropriate for the widget.  Returning
 * to the <code>TRAVERSE_TAB_NEXT</code> example, a text widget implemented
 * by a canvas, would typically want to use the tab key to insert a
 * tab character into the widget.  A list widget implementation, on the
 * other hand, would like the system default traversal behavior.  Using
 * only the doit flag, both implementations are possible.  The text widget
 * implementor sets doit to <code>false</false>, ensuring that the system
 * will not traverse and that the tab key will be delivered to key listeners.
 * The list widget implementor sets doit to <code>true</code>, indicating
 * that the system should perform tab traversal and that the key should not
 * be delivered to the list widget.
 * </p><p>
 * How can the traversal event be used to override system traversal?
 * When the return key is pressed in a single line text control, the
 * detail field is be <code>TRAVERSE_RETURN</code> and the doit field
 * is <code>true</code>.  This means that the return key will processed
 * by the default button, not the text widget.  If the text widget has
 * a default selection listener, it will not run because the return key
 * will be processed by the default button.  Imagine that the text control
 * is being used as an in-place editor and return is used to dispose the
 * widget.  Setting doit to <code>false</code> will stop the system from
 * activating the default button but the key will be delivered to the text
 * control, running the key and selection listeners for the text.  How
 * can <code>TRAVERSE_RETURN be implemented so that the default button
 * will not be activated and the text widget will not see the return key?
 * This is achieved by setting doit to <code>true</code>, and the detail
 * to <code>TRAVERSE_NONE</code>.
 * </p><p>
 * Note: A widget implementor will typically implement traversal using
 * only the doit flag to either enable or disable system traversal.
 * </p>
 * 
 * @see TraverseListener
 */

public class TraverseEvent : KeyEvent {
	
	/**
	 * The traversal type.
	 * <p><ul>
	 * <li>{@link org.eclipse.dwt.DWT#TRAVERSE_NONE}</li>
	 * <li>{@link org.eclipse.dwt.DWT#TRAVERSE_ESCAPE}</li>
	 * <li>{@link org.eclipse.dwt.DWT#TRAVERSE_RETURN}</li>
	 * <li>{@link org.eclipse.dwt.DWT#TRAVERSE_TAB_NEXT}</li>
	 * <li>{@link org.eclipse.dwt.DWT#TRAVERSE_TAB_PREVIOUS}</li>
	 * <li>{@link org.eclipse.dwt.DWT#TRAVERSE_ARROW_NEXT}</li>
	 * <li>{@link org.eclipse.dwt.DWT#TRAVERSE_ARROW_PREVIOUS}</li>
	 * <li>{@link org.eclipse.dwt.DWT#TRAVERSE_MNEMONIC}</li>
	 * <li>{@link org.eclipse.dwt.DWT#TRAVERSE_PAGE_NEXT}</li>
	 * <li>{@link org.eclipse.dwt.DWT#TRAVERSE_PAGE_PREVIOUS}</li>
	 * </ul></p>
	 * 
	 * Setting this field will change the type of traversal.
	 * For example, setting the detail to <code>TRAVERSE_NONE</code>
	 * causes no traversal action to be taken.
	 * 
	 * When used in conjuction with the <code>doit</code> field, the
	 * traversal detail field can be useful when overriding the default
	 * traversal mechanism for a control. For example, setting the doit
	 * field to <code>false</code> will cancel the operation and allow
	 * the traversal key stroke to be delivered to the control. Setting
	 * the doit field to <code>true</code> indicates that the traversal
	 * described by the detail field is to be performed.
	 */
	public int detail;
	
/**
 * Constructs a new instance of this class based on the
 * information in the given untyped event.
 *
 * @param e the untyped event containing the information
 */
public this(Event e) {
	super(e);
	this.detail = e.detail;
}

/**
 * Returns a string containing a concise, human-readable
 * description of the receiver.
 *
 * @return a string representation of the event
 */
public char[] toString() {
	
	char[] string = (super.toString())[0..--$];// remove trailing '}'
	char[] s = string ~ " detail=" ~Int.toString( detail )~ "}";
	return s;
}
}


/**
 * Instances of this class are sent as a result of
 * trees being expanded and collapsed.
 *
 * @see TreeListener
 */

public class TreeEvent : SelectionEvent {

/**
 * Constructs a new instance of this class based on the
 * information in the given untyped event.
 *
 * @param e the untyped event containing the information
 */
public this(Event e) {
	super(e);
}

}




/**
 * Instances of this class are sent as a result of
 * widgets handling keyboard events
 *
 * @see VerifyListener
 */

public class VerifyEvent : KeyEvent {
	
	/**
	 * the range of text being modified.
	 * Setting these fields has no effect.
	 */
	public int start, end;
	
	/**
	 * the new text that will be inserted.
	 * Setting this field will change the text that is about to
	 * be inserted or deleted.
	 */
	public char[] text;

/**
 * Constructs a new instance of this class based on the
 * information in the given untyped event.
 *
 * @param e the untyped event containing the information
 */
public this(Event e) {
	super(e);
	this.character = e.character;
	this.keyCode = e.keyCode;
	this.stateMask = e.stateMask;
	this.start = e.start;
	this.end = e.end;
	this.text = e.text;
}

/**
 * Returns a string containing a concise, human-readable
 * description of the receiver.
 *
 * @return a string representation of the event
 */
public char[] toString() {
	
	char[] string = (super.toString())[0..--$];// remove trailing '}'
	char[] s = string  ~
		" start="  ~ Int.toString( start )
		~ " end=" ~  Int.toString( end )
		~ " text=" ~ text  
		~ "}";
	return s;
}
}


